package com.zbbt.sportslottery.ui.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.bean.ActionCenter;
import com.zbbt.sportslottery.ui.activity.ActionInfoActivity;
import com.zbbt.sportslottery.ui.activity.LoginActivity;
import com.zbbt.sportslottery.ui.activity.RedBookActivity;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.ScreenUtil;

public class ActionCenterAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater inflater;
	private List<ActionCenter> list;
	private Handler handler;

	public ActionCenterAdapter(Context context, List<ActionCenter> list, Handler handler) {
		this.context = context;
		this.list = list;
		inflater = LayoutInflater.from(context);
		this.handler = handler;

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ActionCenter entity = list.get(position);
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_action_pic, null);
			vh = new ViewHolder();
			vh.title = (TextView) convertView.findViewById(R.id.action_big_pic_title);
			vh.head = (ImageView) convertView.findViewById(R.id.action_big_pic);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		vh.head.setImageResource(R.drawable.img_load_failbig);
		vh.title.setText(entity.getActivityName());
		vh.url = entity.getActivityUrl();
		LayoutParams params = null;
		int windowWidth = App.SCREEN_WIDTH - ScreenUtil.dip2px(context, 20);
		int widowHeight = 0, imgh = entity.getHeight(), imgw = entity.getWidth();
		if (imgh == 0 || imgw == 0) {
			imgh = 200;
			imgw = 200;
		}
		widowHeight = (windowWidth * imgh) / imgw;
		params = new LayoutParams(windowWidth, widowHeight);
		vh.head.setLayoutParams(params);
		ImageLoader.getInstance().displayImage(entity.getActivityImg(), vh.head);// 通过
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				int title = entity.getActivityId();
				if (title==12) {
					if (App.isLogin == true) {
						Intent intentd = new Intent(context, RedBookActivity.class);
						intentd.putExtra("Url", entity.getActivityUrl());
						context.startActivity(intentd);
					} else {
						Intent in = new Intent(context, LoginActivity.class);
						context.startActivity(in);
					}
				} else {
					Intent intent = new Intent(context, ActionInfoActivity.class);
					intent.putExtra("isOpen", 1);
					intent.putExtra("Title", "活动");
					intent.putExtra("ShowShare", true);
					if (entity.getActivityImg() != null) {
						intent.putExtra("ImageUrl", entity.getActivityImg());
					}
					intent.putExtra("ShareTitle", entity.getActivityName());
					intent.putExtra("SummaryTx", entity.getActivityRemark());
					intent.putExtra("Url", entity.getActivityUrl());
					context.startActivity(intent);
				}
			}
		});
		if (handler != null && position == list.size() - 1) {
			handler.sendEmptyMessage(Constant.HAS_MORE);
		}
		return convertView;
	}

	static class ViewHolder {
		public TextView title;
		public ImageView head;
		public String url;
	}
}
